package SoftwareEngineering01.WordFrequence.Step0;

import java.io.IOException;
import java.util.Arrays;

public class Counting {
    public static void counting(String strings) throws IOException {
        int sum = 0;
        Word [] words = new Word[30];
        for (int i = 0; i < 26; i++) {
            words[i] = new Word();
            words[i].index = i;
            words[i].value = 0;
        }
        String s = "abcdefghijklmnopqrstuvwxyz";
        for (int i = 0; i < strings.length(); i++) {
            for (int j = 0; j < s.length(); j++) {
                if(strings.charAt(i) == s.charAt(j) || strings.charAt(i) == s.charAt(j) - 32){
                    words[s.charAt(j) - 'a'].value++;
                    sum++;
                    break;
                }
            }
        }
        Arrays.sort(words);
        for (int i = 0; i < 26; i++) {
            double per = words[i].value*1.0 / sum * 100;
            System.out.printf("%c的频率为%.2f%%\n",words[i].index + 'a',per);
        }
    }
}
